package custom.base.utils;

import android.content.Context;
import android.widget.ImageView;

import androidx.annotation.DrawableRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.RequestOptions;

import custom.base.R;


public class GlideUtils {
    static GlideUtils instance = null;
    private Glide glide = null;
    private Context context;

    public static GlideUtils getInstance() {
        synchronized (GlideUtils.class) {
            if (instance == null)
                instance = new GlideUtils();
            return instance;
        }
    }

    public void init(Context context) {
        this.context = context;
        glide = Glide.get(context);
    }

    public Glide getGlide() {
        return glide;
    }

    public GlideUtils() {

    }

    /**
     * 显示普通图片
     */
    public void displayImageView(String url, @NonNull ImageView view) {
        displayImageView(url, view, null, R.drawable.rect_loading_bg);
    }

    /**
     * 显示普通图片
     */
    public void displayImageView(String url, @NonNull ImageView view, @DrawableRes int resourceId) {
        displayImageView(url, view, null, resourceId);
    }

    /**
     * 显示普通图片
     */
    public void displayImageView(String url, @NonNull ImageView view, @Nullable RequestListener requestListener, @DrawableRes int resourceId) {
        Glide.with(context).load(url).apply(getOptionsForImgList(resourceId))
                .transition(getTransitionOptions()).listener(requestListener).into(view);
    }

    /**
     * 显示缩略图片
     */
    public void displayThumbnailImageView(String url, @NonNull ImageView view, float thumbnail) {
        displayThumbnailImageView(url, view, null, R.drawable.rect_loading_bg, thumbnail);
    }

    /**
     * 显示缩略图片
     */
    public void displayThumbnailImageView(String url, @NonNull ImageView view, @Nullable RequestListener requestListener, @DrawableRes int resourceId, float thumbnail) {
        Glide.with(context).load(url).apply(getOptionsForImgList(resourceId)).thumbnail(thumbnail)
                .transition(getTransitionOptions()).listener(requestListener).into(view);
    }

    /**
     * 显示普通头像图片
     */
    public void displayPortrait(String url, @NonNull ImageView view) {
        displayPortrait(url, view, R.mipmap.avatar_default);
    }

    /**
     * 显示普通头像图片
     */
    public void displayPortrait(String url, @NonNull ImageView view, @DrawableRes int resourceId) {
        Glide.with(context).load(url).apply(getOptionsForPortrait(resourceId))
                .transition(getTransitionOptions()).into(view);
    }


    /**
     * 普通列表图片配置
     */
    public RequestOptions getOptionsForImgList(@DrawableRes int resourceId) {
        RequestOptions requestOptions = new RequestOptions()
                .placeholder(resourceId)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .encodeQuality(90);

        return requestOptions;
    }

    /**
     * 普通头像图片配置
     */
    public RequestOptions getOptionsForPortrait(@DrawableRes int resourceId) {
        RequestOptions requestOptions = new RequestOptions()
                .placeholder(resourceId)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .encodeQuality(90);

        return requestOptions;
    }


    public DrawableTransitionOptions getTransitionOptions() {
        DrawableTransitionOptions transitionOptions = new DrawableTransitionOptions()
                .crossFade();
        return transitionOptions;
    }

    /**
     * 暂定加载
     */
    public void pauseRequests() {
        Glide.with(context).pauseRequests();
    }

    /**
     * 恢复加载
     */
    public void resumeRequests() {
        Glide.with(context).resumeRequests();
    }
}
